<?php

    /**
     * Http
     *
     * @package      Core
     * @subpackage   Controller_Response
     *
     * @license      GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html
     */
    class Core_Controller_Response_Download extends Core_Controller_Response_Abstract
    {

	////////////////////////////////////////////////////////////////////////////
	//                                Variables                               //
	////////////////////////////////////////////////////////////////////////////

        /**
        * The response type
        *
        * @var string
        */
        protected $_type = 'download';

        /**
         * File path
         *
         * @var string
         */
        protected $_file = null;

        /**
         * File name
         *
         * @var string
         */
        protected $_displayName = null;

        /**
         * Singleton instance
         *
         * Marked only as protected to allow extension of the class.
         * To extend, simply override {@link getInstance()}.
         *
         * @var Core_Controller_Response_Download
         */
        protected static $_instance = null;

	////////////////////////////////////////////////////////////////////////////
	//                            Getters / Setters                           //
	////////////////////////////////////////////////////////////////////////////

        /**
         * Sets the file to download
         *
         * @param       string      $path : file to download
		 * @throws		Core_ArgumentException
         * @return      Core_Controller_Response_Download
         */
        public function setFile($path)
        {
            if (!Core_IO_File::exists($path)) {
                throw new Core_Exception('Fatal error : File must exists');
            }

            $this->_file = $path;

            return $this;
        }

        /**
         * Sets the displayed name
         *
         * @param       string      $name
         * @return      Core_Controller_Response_Download
         */
        public function setName($name)
        {
            $this->_displayName = $name;

            return $this;
        }

	////////////////////////////////////////////////////////////////////////////
	//                             Static Methods                             //
	////////////////////////////////////////////////////////////////////////////

        /**
         * Singleton instance
         * Returns the current instance (used to access the response from the template)
         *
         * @return      Core_Controller_Response_Download|null
         */
        public static function getInstance()
        {
            return self::$_instance;
        }

	////////////////////////////////////////////////////////////////////////////
	//                                 Methods                                //
	////////////////////////////////////////////////////////////////////////////

        /**
         * Constructor
         *
         * Sets the charset, the language and the template engine
         */
        public function __construct()
        {
            parent::__construct();

            self::$_instance = $this;
        }

        /**
         * Outputs the HTML content
         *
         * @throws      Core_Exception
         * @return      boolean     true if the generated content is ok
         */
        public final function output()
        {
            $this->_httpHeadersSent = false;

            if ($this->_file === null) {
                throw new Core_Exception('Fatal error : No file specified');
            }

            $this->_httpHeaders['Content-Type']                 = 'application/force-download; name="'. $this->_file .'"';
            $this->_httpHeaders['Content-Transfer-Encoding']    = 'binary';
            $this->_httpHeaders['Content-Length']               = Core_IO_File::size($this->_file);
            $this->_httpHeaders['Content-Disposition']          = 'attachment; filename="'. (($this->_displayName === null) ? Core_IO_Path::getFileName($this->_file) : $this->_displayName) .'"';
            $this->_httpHeaders['Expires']                      = '0';
            $this->_httpHeaders['Cache-Control']                = 'no-cache, must-revalidate';
            $this->_httpHeaders['Pragma']                       = 'no-cache';

            $this->sendHttpHeaders();

            echo Core_IO_File::readAll($this->_file);

            return true;
        }

        /**
         * Outputs the errors
         *
         * @return      void
         */
        public final function outputErrors()
        {}
    }
